<?php
/**
 * @date 2012-4-8
 * @author GhostWei
 */
class controller_admin_supplier extends system_controller_action {
	
	private $user = null;
	private $referer = null;
	private $language = null;
	public function init() {
		if($this->adminLogin()) {
			return;
		}
		$this->view->language = $this->language = $this->view->language();
		$this->view->user = $this->user = system_user::getInstance()->getRow();
		$this->view->referer = $this->referer = $this->getRequest()->getHeader('referer');
		
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Supplier'))->setRoute('supplier/index');
		$this->crumbs()->addPage($page);
	}
	
	public function action_index() {
		$this->redirector()->gotoRouteAndExit('supplier/list');
		$this->render();
	}
	
	public function action_detail() {
		$id = intval($this->getRequest()->getParam('id'));
		$this->redirector()->gotoRouteAndExit('supplier/edit', array('id' => $id));
		$this->render();
	}
	
	public function action_list() {
		$concept = new supplier_information_concept();
		$this->view->list = $concept->fetchAll();
		
		$page = new system_nav_page();
		$page->setLabel($this->language->load('List'))->setRoute('supplier/list');
		$this->crumbs()->addPage($page);
		$this->render();
	}
	
	public function action_addnew() {
		$concept = new supplier_information_concept();
		$row = $concept->create();
		$row->setIsActive(1);
		
		$page = new system_nav_page();
		$page->setLabel($this->language->load('New Supplier'))->setRoute('supplier/addnew');
		$this->_render($row, $page);
	}
	
	public function action_edit() {
		$id = intval($this->getRequest()->getParam('id'));
		$concept = new supplier_information_concept();
		if(!($row = $concept->fetch(array('supplier.id = ?' => $id)))) {
			throw new supplier_exception("Supllier is not Existing.");
		}
		$page = new system_nav_page();
		$page->setLabel($row->getTitle())->setRoute('supplier/edit')->setParams(array('id' => $id));
		$this->_render($row, $page);
	}
	
	public function _render($row, system_nav_page $lpage) {
		if($this->getRequest()->isPost()) {
			$this->referer = $this->getRequest()->getPost('referer');
			$name = trim($this->getRequest()->getPost('name'));
			$row->setName(preg_replace('/[\s]+/is', '', $name));
			$row->setTitle(trim($this->getRequest()->getPost('title')));
			$row->setCorporation(trim($this->getRequest()->getPost('corporation')));
			$row->setLicence(trim($this->getRequest()->getPost('licence')));
			$row->setWebsite(trim($this->getRequest()->getPost('website')));
			$row->setIsActive(intval($this->getRequest()->getPost('is_active')));
			$row->setDescription($this->getRequest()->getPost('description'));			
			try {
				system_db::getAdapter()->beginTransaction();
				if(!empty($_FILES["thumb"]['tmp_name'])) {
					common_media_config::$imageFilter = array('jpg', 'png', 'gif');
					$thumb = common_media_image::create($_FILES["thumb"]);
					if($thumb) {
						$row->setLogoMd5($thumb->getMd5());
					}
					$thumb->save();
				}
				if($this->getRequest()->getPost('deleteImage')) {
					$row->setLogoMd5('');
				}
				$row->save();
				system_db::getAdapter()->commit();
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				system_db::getAdapter()->rollBack();
				$this->view->throwException = $e;
			}
		}
		$this->view->referer = $this->referer;
		$this->view->row = $row;
		$page = new system_nav_page();
		$page->setLabel($this->language->load('List'))->setRoute('supplier/list');
		$this->crumbs()->addPage($page);
		$this->crumbs()->addPage($lpage);
		$this->render();
	}
}